package com.lin.lazybone.ui;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.lin.lazybone.R;
import com.lin.lazybone.adapter.DialogLVAdapter;
import com.lin.lazybone.entity.DelType;
import com.lin.lazybone.entity.Order;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.utils.StaticClass;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class GetDeliveryActivity extends AppCompatActivity implements View.OnClickListener
{
    //获取各个控件
    private ImageView express_Image;
    private EditText from_address;
    private EditText to_address;
    private EditText need_price;
    private EditText need_content;
    private Button need_bt_OK;
    private CustomDialog dialog;
    private ListView dialog_listView;
    private List<DelType> mDelType;
    private _User user;

    //保存快递类型
    private String orderDelType = "";


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_delivery);
        Toolbar toolbar = (Toolbar) findViewById(R.id.getdel_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        initData();
        initView();


    }

    private void initData()
    {
        user = BmobUser.getCurrentUser(_User.class);
        //初始化快递信息
        mDelType = new ArrayList<>();
        mDelType.add(new DelType("顺丰", R.mipmap.sf));
        mDelType.add(new DelType("申通", R.mipmap.sto));
        mDelType.add(new DelType("中通", R.mipmap.zto));
        mDelType.add(new DelType("ems", R.mipmap.ems));
        mDelType.add(new DelType("百世汇通", R.mipmap.best));
        mDelType.add(new DelType("圆通", R.mipmap.yto));
        mDelType.add(new DelType("韵达", R.mipmap.yunda));

    }

    private void initView()
    {
        //初始化各控件
        express_Image = (ImageView) findViewById(R.id.express_Image);
        express_Image.setOnClickListener(this);
        from_address = (EditText) findViewById(R.id.from_address);
        to_address = (EditText) findViewById(R.id.to_address);
        need_price = (EditText) findViewById(R.id.need_price);
        need_content = (EditText) findViewById(R.id.need_content);
        need_bt_OK = (Button) findViewById(R.id.need_bt_OK);
        need_bt_OK.setOnClickListener(this);
        dialog = new CustomDialog(this, 0, 1000, R.layout.dialog_deltype,
                R.style.Theme_dialog, Gravity.BOTTOM, R.style.pop_anim_style);
        dialog_listView = (ListView) dialog.findViewById(R.id.dialog_listView);
        DialogLVAdapter adapter = new DialogLVAdapter(this, mDelType);
        dialog_listView.setAdapter(adapter);
        dialog_listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                DelType delType = mDelType.get(position);
                orderDelType = delType.getType();
                express_Image.setBackgroundResource(delType.getImageID());
                dialog.dismiss();
            }
        });


    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            //点击了确定发布按钮
            case R.id.need_bt_OK:
                String from = from_address.getText().toString();
                String to = to_address.getText().toString();
                String price = need_price.getText().toString();
                String content = need_content.getText().toString();
                if (!TextUtils.isEmpty(orderDelType) && !TextUtils.isEmpty(from) && !TextUtils.isEmpty(to) && !TextUtils.isEmpty(price))
                {
                    Order order = new Order();
                    order.setOrder_state(1);
                    order.setOrder_del_type(orderDelType);
                    order.setOrder_from(from);
                    order.setOrder_to(to);
                    order.setOrder_price(price);
                    order.setOrder_content(content);
                    //之后通过ShareUtil从存储中获取到用户账号
                    order.setOrder_aid(user.getUsername());

                    order.setOrder_uid("");
                    order.setOrder_type(StaticClass.DEL_TYPE);
                    order.save(new SaveListener<String>()
                    {
                        @Override
                        public void done(String s, BmobException e)
                        {
                            Toast.makeText(GetDeliveryActivity.this, "成功发布！", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else
                {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            //点击了快递类型图片
            case R.id.express_Image:
                dialog.show();
                break;

        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
